﻿/****************************************************************************
*	Copyright (c) 2013, changyuan.game
*	All rights reserved.
*****************************************************************************
*	filename:	RapidJsonHelper.cpp
*  author:		ye.tao
*	e-mail:		yetao@11game.com
*	created:	2014/06/16
*	descript:  rapidJson数据读取与写入封装
****************************************************************************/
#include "RapidJsonHelper.h"

RapidJsonHelper::RapidJsonHelper()
{

}

RapidJsonHelper::~RapidJsonHelper()
{

}

std::string RapidJsonHelper::transJsonToString( const rapidjson::Value& root )
{
	rapidjson::StringBuffer buffer;
	rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
	root.Accept(writer);
	std::string str= buffer.GetString();
	return str;
}

void RapidJsonHelper::transStringToJson( const std::string& str, rapidjson::Document & resultJson )
{
	resultJson.Parse<0>(str.c_str());
	return;
}

int RapidJsonHelper::getInt(  const char* key, const rapidjson::Value& root )
{
	int nRet = 0;
	do {
		CC_BREAK_IF(root.IsNull());
		CC_BREAK_IF(root[key].IsNull());
		nRet = root[key].GetInt();
	} while (0);

	return nRet;
}

int64_t RapidJsonHelper::getInt64( const char* key, const rapidjson::Value& root )
{
	int64_t nRet = 0;
	do {
		CC_BREAK_IF(root.IsNull());
		CC_BREAK_IF(root[key].IsNull());
		nRet = root[key].GetInt64();
	} while (0);

	return nRet;
}

uint64_t RapidJsonHelper::getUint64( const char* key, const rapidjson::Value& root )
{
	uint64_t nRet = 0;
	do {
		CC_BREAK_IF(root.IsNull());
		CC_BREAK_IF(root[key].IsNull());
		nRet = root[key].GetUint64();
	} while (0);

	return nRet;
}

float RapidJsonHelper::getFloat( const char* key, const rapidjson::Value& root )
{
	float nRet = 0;
	do {
		CC_BREAK_IF(root.IsNull());
		CC_BREAK_IF(root[key].IsNull());
		nRet = root[key].GetDouble();
	} while (0);

	return nRet;
}

bool RapidJsonHelper::getBool( const char* key, const rapidjson::Value& root )
{
	bool nRet = 0;
	do {
		CC_BREAK_IF(root.IsNull());
		CC_BREAK_IF(root[key].IsNull());
		nRet = root[key].GetBool();
	} while (0);

	return nRet;
}

const char* RapidJsonHelper::getCString( const char* key, const rapidjson::Value& root )
{
	const char* nRet = 0;
	do {
		CC_BREAK_IF(root.IsNull());
		CC_BREAK_IF(root[key].IsNull());
		nRet = root[key].GetString();
	} while (0);

	return nRet;
}

std::string RapidJsonHelper::getString( const char* key, const rapidjson::Value& root )
{
	std::string str ;
	do {
		CC_BREAK_IF(root.IsNull());
		CC_BREAK_IF(root[key].IsNull());
		str	= root[key].GetString();
	} while (0);
	return str;
}

int RapidJsonHelper::getArrayCount( const char* key, const rapidjson::Value& root )
{
	int nCount = 0;
	do {
		CC_BREAK_IF(root.IsNull());
		CC_BREAK_IF(root[key].IsNull());
		nCount = root[key].Size();
	} while (0);

	return nCount;
}

void RapidJsonHelper::addInt( const char * key, const int value, rapidjson::Document & document )
{
	rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
	document.AddMember(key, value, allocator);
	return;
}

void RapidJsonHelper::addInt64( const char * key, const int64_t value, rapidjson::Document & document )
{
	rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
	document.AddMember(key, value, allocator);
	return;
}

void RapidJsonHelper::addUint64( const char * key, const uint64_t value, rapidjson::Document & document )
{
	rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
	document.AddMember(key, value, allocator);
	return;
}

void RapidJsonHelper::addFloat( const char * key, const float value, rapidjson::Document & document )
{
	rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
	document.AddMember(key, value, allocator);
	return;
}

void RapidJsonHelper::addBool( const char * key, const bool value, rapidjson::Document & document )
{
	rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
	document.AddMember(key, value, allocator);
	return;
}

void RapidJsonHelper::addCString( const char * key, const char* value, rapidjson::Document & document )
{
	rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
	document.AddMember(key, value, allocator);
	return;
}

void RapidJsonHelper::addString( const char * key, const std::string value, rapidjson::Document & document )
{
	rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
	document.AddMember(key, value.c_str(), allocator);
	return;
}









